package com.mike.bdb.ui;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mike.bdb.util.DataCurrent;
import com.mike.bdb.util.ImageManager;
import com.mike.swt.util.ShellUtil;

public class About {

	private static final Logger LOG = LoggerFactory.getLogger(About.class);
	
	private Shell motherland;
	protected Shell shell;

	private volatile boolean open = false;
	private FormData formData_1;
	private Label logo;
	private Label label;
	private FormData formData;
	private static final Cursor hand = new Cursor(Display.getDefault(), SWT.CURSOR_HAND);
	
	public About(Shell motherland) {
		this.motherland = motherland;
	}
	
	public boolean isOpen() {
		return open;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			About window = new About(null);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		open = true;
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		open = false;
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.BORDER | SWT.APPLICATION_MODAL);
		shell.setImage(ImageManager.getImage(ImageManager.LOGO_MING));
		shell.setSize(644, 340);
		shell.setText(Messages.getString("Motherland.motherland.text")
				+ " " 
				+ DataCurrent.VERSION ); //$NON-NLS-1$
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			{
				{
					Composite composite = new Composite(shell, SWT.NONE);
					composite.setLayout(new FormLayout());
					{
						logo = new Label(composite, SWT.NONE);
						{
							formData_1 = new FormData();
							formData_1.left = new FormAttachment(0, 24);
							formData_1.bottom = new FormAttachment(100, -41);
							formData_1.top = new FormAttachment(0, 38);
							logo.setLayoutData(formData_1);
						}
						Image image = ImageManager.getImage(ImageManager.ICON_MIKE);
						logo.setImage(image);
						logo.setCursor(hand);
						logo.setToolTipText(Messages.getString("copyright"));
						logo.addMouseListener(new MouseAdapter() {

							@Override
							public void mouseDown(MouseEvent e) {
								openWebSite();
							}
							
						});
					}
					{
						label = new Label(composite, SWT.NONE);
						formData_1.right = new FormAttachment(100, -363);
						{
							formData = new FormData();
							formData.bottom = new FormAttachment(100, -147);
							formData.left = new FormAttachment(logo, 84);
							label.setLayoutData(formData);
						}
						label.setText(Messages.getString("Motherland.motherland.text")
								+ " " 
								+ DataCurrent.VERSION); //$NON-NLS-1$
					}
					{
						Link link = new Link(composite, SWT.NONE);
						link.setLayoutData(new FormData());
						link.setFocus();
					}
					
				}
			}
		}
		if (null != motherland) {
			ShellUtil.setCenter(shell, motherland);
		}
	}
	
	private void openWebSite() {
		try {
			String web = Messages.getString("copyright.web");
			Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + web);
			if(LOG.isDebugEnabled()) {
				LOG.debug(this.getClass().getSimpleName() + " > " + web);
			}
		} catch (IOException ex) {
		}
	}
	
	
}
